﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class 查询书籍 : Form
    {
        String key;
        String content;
        List<books> Books;
        int method;
        public 查询书籍(String key, String content)
        {
            InitializeComponent();
            this.key = key;
            this.content = content;

        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void 查询书籍_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedItem = key;
            if (key == "关键字")
            {
                this.method = 1;
            }
            if (key == "ISBN")
            {
                this.method = 2;
            }
            if (key == "作者名")
            {
                this.method = 3;
            }

            comboBox1.Text = key;
            textBox1.Text = content;
            BookDao bookDao = new BookDao();
            if(content!="")
            {
                Books = bookDao.findbooklei(method, content);
                books books = new books();
                label5.Text = Books.Count.ToString();
                label3.Text = ((int)Math.Ceiling(Books.Count * 1.0 / 4)).ToString();
                label1.Text = "1";
                int k = Books.Count;
                if (Books.Count > 4)
                {
                    k = 4;
                }
                for (int i = 0; i < k; i++)
                {
                    books = Books[i];
                    String gboxname = "书名:";
                    GroupBox gbox = new GroupBox();
                    gbox.Name = i.ToString();
                    gbox.Text = gboxname + books.BookName1;
                    gbox.Width = 719;
                    gbox.Height = 106;
                    gbox.Location = new Point(9, 82 + i * 133);
                    this.Controls.Add(gbox);
                    //调用添加文本控件的方法
                    String Authorname = "作者:";
                    Label Author = new Label();
                    Author.Name = Authorname + books.Author1 + i;
                    Author.Text = Authorname + books.Author1;
                    Author.Location = new Point(158, 28);

                    String Pressname = "出版社:";
                    Label Press = new Label();
                    Press.Name = Pressname + books.Press1 + i;
                    Press.Text = Pressname + books.Press1;
                    Press.Location = new Point(306, 28);

                    String Booknumber = "图书数量:";
                    Label Booknum = new Label();
                    Booknum.Name = Booknumber + books.Sumbook + i;
                    Booknum.Text = Booknumber + books.Sumbook;
                    Booknum.Location = new Point(158, 76);

                    String kejienumber = "可借数量:";
                    Label kejienum = new Label();
                    kejienum.Name = kejienumber + books.Kejiesum + i;
                    kejienum.Text = kejienumber + books.Kejiesum;
                    kejienum.Location = new Point(306, 76);

                    Button jieyue = new Button();
                    jieyue.Name = i.ToString();
                    jieyue.Text = "查看详情/借阅";
                    jieyue.Location = new Point(526, 63);
                    jieyue.Width = 163;
                    jieyue.Height = 39;
                    jieyue.Click += new EventHandler(jieyue_Click);

                    gbox.Controls.Add(Author);
                    gbox.Controls.Add(Press);
                    gbox.Controls.Add(Booknum);
                    gbox.Controls.Add(kejienum);
                    gbox.Controls.Add(jieyue);
                }
            }
            
            
        }

        public void AddGroupBox()
        {
            String name = "书名：";

        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (label1.Text == "1")
            {
                MessageBox.Show("已经是第一页了！");
            }
            else
            {
                for (int j = 0; j < 4; j++)
                {
                    foreach (Control i in this.Controls)
                    {
                        if (i is GroupBox)
                        {
                            this.Controls.Remove(i);
                            i.Dispose();
                        }
                    }
                }
                int currentPage = int.Parse(label1.Text);
                currentPage = currentPage - 1;
                label1.Text = currentPage.ToString();
                BookDao bookDao = new BookDao();
                Books = bookDao.findbooklei(method, content);
                books books = new books();
                for (int i = 4 * (currentPage - 1); i < currentPage * 4; i++)
                {
                    books = Books[i];
                    String gboxname = "书名:";
                    GroupBox gbox = new GroupBox();
                    gbox.Name = i.ToString();
                    gbox.Text = gboxname + books.BookName1;
                    gbox.Width = 719;
                    gbox.Height = 106;
                    gbox.Location = new Point(9, 82 + (i - 4 * (currentPage - 1)) * 133);
                    this.Controls.Add(gbox);
                    //调用添加文本控件的方法
                    String Authorname = "作者:";
                    Label Author = new Label();
                    Author.Name = Authorname + books.Author1 + i;
                    Author.Text = Authorname + books.Author1;
                    Author.Location = new Point(158, 28);

                    String Pressname = "出版社:";
                    Label Press = new Label();
                    Press.Name = Pressname + books.Press1 + i;
                    Press.Text = Pressname + books.Press1;
                    Press.Location = new Point(306, 28);

                    String Booknumber = "图书数量:";
                    Label Booknum = new Label();
                    Booknum.Name = Booknumber + books.Sumbook + i;
                    Booknum.Text = Booknumber + books.Sumbook;
                    Booknum.Location = new Point(158, 76);

                    String kejienumber = "可借数量:";
                    Label kejienum = new Label();
                    kejienum.Name = kejienumber + books.Kejiesum + i;
                    kejienum.Text = kejienumber + books.Kejiesum;
                    kejienum.Location = new Point(306, 76);

                    Button jieyue = new Button();
                    jieyue.Name = i.ToString();
                    jieyue.Text = "查看详情/借阅";
                    jieyue.Location = new Point(526, 63);
                    jieyue.Width = 163;
                    jieyue.Height = 39;
                    jieyue.Click += new EventHandler(jieyue_Click);

                    gbox.Controls.Add(Author);
                    gbox.Controls.Add(Press);
                    gbox.Controls.Add(Booknum);
                    gbox.Controls.Add(kejienum);
                    gbox.Controls.Add(jieyue);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (label1.Text == label3.Text)
            {
                MessageBox.Show("已经是最后一页了！");
            }
            else
            {
                for (int j = 0; j < 4; j++)
                {
                    foreach (Control i in this.Controls)
                    {
                        if (i is GroupBox)
                        {
                            this.Controls.Remove(i);
                            i.Dispose();
                        }
                    }
                }
                int currentPage = int.Parse(label1.Text);
                currentPage += 1;
                label1.Text = currentPage.ToString();
                BookDao bookDao = new BookDao();
                Books = bookDao.findbooklei(method, content);
                int k = Books.Count;
                if (k > 4 * currentPage)
                {
                    k = 4 * currentPage;
                }
                books books = new books();
                for (int i = 4 * (currentPage - 1); i < k; i++)
                {
                    books = Books[i];
                    String gboxname = "书名:";
                    GroupBox gbox = new GroupBox();
                    gbox.Name = i.ToString();
                    gbox.Text = gboxname + books.BookName1;
                    gbox.Width = 719;
                    gbox.Height = 106;
                    gbox.Location = new Point(9, 82 + (i - 4 * (currentPage - 1)) * 133);
                    this.Controls.Add(gbox);
                    //调用添加文本控件的方法
                    String Authorname = "作者:";
                    Label Author = new Label();
                    Author.Name = Authorname + books.Author1 + i;
                    Author.Text = Authorname + books.Author1;
                    Author.Location = new Point(158, 28);

                    String Pressname = "出版社:";
                    Label Press = new Label();
                    Press.Name = Pressname + books.Press1 + i;
                    Press.Text = Pressname + books.Press1;
                    Press.Location = new Point(306, 28);

                    String Booknumber = "图书数量:";
                    Label Booknum = new Label();
                    Booknum.Name = Booknumber + books.Sumbook + i;
                    Booknum.Text = Booknumber + books.Sumbook;
                    Booknum.Location = new Point(158, 76);

                    String kejienumber = "可借数量:";
                    Label kejienum = new Label();
                    kejienum.Name = kejienumber + books.Kejiesum + i;
                    kejienum.Text = kejienumber + books.Kejiesum;
                    kejienum.Location = new Point(306, 76);

                    Button jieyue = new Button();
                    jieyue.Name =i.ToString();
                    jieyue.Text = "查看详情/借阅";
                    jieyue.Location = new Point(526, 63);
                    jieyue.Width = 163;
                    jieyue.Height = 39;
                    jieyue.Click += new EventHandler(jieyue_Click);

                    gbox.Controls.Add(Author);
                    gbox.Controls.Add(Press);
                    gbox.Controls.Add(Booknum);
                    gbox.Controls.Add(kejienum);
                    gbox.Controls.Add(jieyue);
                }
            }
        }

        private void jieyue_Click(object sender, EventArgs e)
        {
           
            int detail_bookname = int.Parse(((Button)sender).Name);
            books books = Books[detail_bookname];
            书籍详情 form = new 书籍详情(books,method,content);
            form.Show();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < 4; j++)
            {
                foreach (Control i in this.Controls)
                {
                    if (i is GroupBox)
                    {
                        this.Controls.Remove(i);
                        i.Dispose();
                    }
                }
            }
            String key = comboBox1.Text;
            if (key == "关键字")
            {
                this.method = 1;
            }
            if (key == "ISBN")
            {
                this.method = 2;
            }
            if (key == "作者名")
            {
                this.method = 3;
            }

            content = textBox1.Text;
            BookDao bookDao = new BookDao();
            if (content != "")
            {
                Books = bookDao.findbooklei(method, content);
                books books = new books();
                label5.Text = Books.Count.ToString();
                label3.Text = ((int)Math.Ceiling(Books.Count * 1.0 / 4)).ToString();
                label1.Text = "1";
                int k = Books.Count;
                if (Books.Count > 4)
                {
                    k = 4;
                }
                for (int i = 0; i < k; i++)
                {
                    books = Books[i];
                    String gboxname = "书名:";
                    GroupBox gbox = new GroupBox();
                    gbox.Name = i.ToString();
                    gbox.Text = gboxname + books.BookName1;
                    gbox.Width = 719;
                    gbox.Height = 106;
                    gbox.Location = new Point(9, 82 + i * 133);
                    this.Controls.Add(gbox);
                    //调用添加文本控件的方法
                    String Authorname = "作者:";
                    Label Author = new Label();
                    Author.Name = Authorname + books.Author1 + i;
                    Author.Text = Authorname + books.Author1;
                    Author.Location = new Point(158, 28);

                    String Pressname = "出版社:";
                    Label Press = new Label();
                    Press.Name = Pressname + books.Press1 + i;
                    Press.Text = Pressname + books.Press1;
                    Press.Location = new Point(306, 28);

                    String Booknumber = "图书数量:";
                    Label Booknum = new Label();
                    Booknum.Name = Booknumber + books.Sumbook + i;
                    Booknum.Text = Booknumber + books.Sumbook;
                    Booknum.Location = new Point(158, 76);

                    String kejienumber = "可借数量:";
                    Label kejienum = new Label();
                    kejienum.Name = kejienumber + books.Kejiesum + i;
                    kejienum.Text = kejienumber + books.Kejiesum;
                    kejienum.Location = new Point(306, 76);

                    Button jieyue = new Button();
                    jieyue.Name = i.ToString();
                    jieyue.Text = "查看详情/借阅";
                    jieyue.Location = new Point(526, 63);
                    jieyue.Width = 163;
                    jieyue.Height = 39;
                    jieyue.Click += new EventHandler(jieyue_Click);

                    gbox.Controls.Add(Author);
                    gbox.Controls.Add(Press);
                    gbox.Controls.Add(Booknum);
                    gbox.Controls.Add(kejienum);
                    gbox.Controls.Add(jieyue);
                }
            }
            else
            {
                label5.Text = "0";
                label3.Text = "0";
                label1.Text = "0";
            }
        }
    }
}
